package com.bdqn.reflect.servlet;

import com.bdqn.reflect.pojo.User;
import com.bdqn.reflect.util.SimpleBeanConvertor;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * UserServlet
 *
 * @author LILIBO
 * @since 2024/7/2
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        // 获取参数
        // String id = request.getParameter("id");
        // String username = request.getParameter("username");
        // 创建对象
        // Book book = new Book();
        // book.setId(Integer.parseInt(id));
        // book.setUsername(username);

        // 获取页面参数，封装到User对象中
        // 规则：页面 <input type='text' name='username' value=''> --> 实体类 private String username;
        User user = SimpleBeanConvertor.parseRequest(request, User.class);

        System.out.println(user);
    }

}
